// page/details/pages/deputyLeader/deputyLeader.js
const utils = require('../../../../utils/util.js');
let myRequest = require('../../../../common/script/myRequest.js');
let api = require('../../../../common/script/api.js');
let login = require('../../../../common/script/login');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    teamer_id: "",
    dataList: {},
    page: 1, // 页码
    pageSize: 10,
    listData: [],
    mainStatus: false,
    isnoshowCard: true,
    showHeader: false,
    isEnd: true,
  },
  getList() {
    const that = this;
    let data = {
      token: wx.getStorageSync('token'),
      page: that.data.page,
      page_size: that.data.pageSize,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.joinUserBonus, data).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        that.setData({
          listData: res.data.result.record_list
        })
      }
    })
  },
  getData() {
    let that = this;
    let data = {
      token: wx.getStorageSync('token'),
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.qywxteamJoinUserDetail, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        that.setData({
          dataList: res.data.result,
          mainStatus: true
        })
      } else {
        // wx.showModal({
        //   content: res.data.msg,
        //   showCancel: false,
        //   confirmText: '我知道了',
        //   confirmColor: '#ff5151',
        //   success(e) {
        //     //返回tab页面刷新数据
        //     wx.setStorageSync('refresh', true)
        //     //返回优惠券页面刷新数据
        //     wx.setStorageSync('refreshCoupon', true)
        //     wx.navigateBack({
        //       delta: 1
        //     })
        //   }
        // })
      }
    })
  },
  Apply() {
    const that = this;
    wx.switchTab({
      url: `/pages/person/person`
    })
  },
  getStatus() {
    let that = this;
    let data = {
      token: wx.getStorageSync('token'),
      teamer_id: that.data.teamer_id
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.qywxteamJoin, data).then(res => {
      if (res.data.status == 1) {
        that.getData()
        that.getList()
        that.setData({
          showHeader: true
        })
      } else {
        wx.hideLoading();
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(e) {
            if (e.confirm) {
              if (that.data.teamer_id) {
                wx.setStorageSync('teamer_id', that.data.teamer_id);
                that.setData({
                  isnoshowCard: false
                })
              } else {
                wx.switchTab({
                  url: '/pages/person/person'
                })
              }
            }
          }
        })
      }
    })
  },
  //申请会员卡
  toAplicationCard() {
    const that = this;
    wx.redirectTo({
      url: `/page/details/pages/aplicationCard/aplicationCard?teamer_id=${that.data.teamer_id}`
    })
  },
  //绑定实体卡
  toSwapCard() {
    const that = this;
    wx.redirectTo({
      url: `/package/pages/swapCard/swapCard?teamer_id=${that.data.teamer_id}`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    login.checkLogin().then(res => {
      let sceneObj = utils.parseQueryString(options.scene)
      that.setData({
        teamer_id: sceneObj.teamer_id || options.teamer_id
      })
      if (sceneObj.teamer_id || options.teamer_id) {
        that.getStatus()
      } else {
        that.getData()
        that.getList()
      }

    }).catch(err => {
      app.appCallBack = (e) => {
        let sceneObj = utils.parseQueryString(options.scene)
        that.setData({
          teamer_id: sceneObj.teamer_id || options.teamer_id
        })
        if (sceneObj.teamer_id || options.teamer_id) {
          that.getStatus()
        } else {
          that.getData()
          that.getList()
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    wx.showNavigationBarLoading()
    that.setData({
      page: 1,
      isEnd: true,
    })

    that.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      return
    }
    let data = {
      token: wx.getStorageSync('token'),
      page: that.data.page + 1,
      page_size: that.data.pageSize,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.joinUserBonus, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.record_list.length == 0) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: false
          })
        } else {
          that.setData({
            listData: [...that.data.listData, ...res.data.result.record_list],
            page: that.data.page + 1
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})